/**
 * AmplandPlugin.java
 * 
 * FreeZzaph is free software; you can redistribute it
 * and/or modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of
 * the License, or (at your option) any later version.
 *
 * FreeZzaph is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.
 */

package freezzaph.plugins;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import freezzaph.exceptions.PluginException;
import freezzaph.exceptions.URLFetchException;

/**
 * FreeZzaph Plugin for the porn portal Ampland.com.
 * 
 * @author FreeZzaph
 */
public final class AmplandPlugin implements FreeZzaphPlugin {
	
	private static final Pattern pictureCategory = Pattern.compile("<a.+?href=\"(/[^/\"]+)\">([^<]+) Pics</a>");
	private static final Pattern movieCategory = Pattern.compile("<a.+?href=\"(/movies/[^/\"]+)\">([^<]+) Movies</a>");
	
	private CategoryList pictures;
	private CategoryList movies;
	private HashMap<String, CategoryList> list;

	/* (non-Javadoc)
	 * @see freezzaph.plugins.FreeZzaphPlugin#getCategoryLists()
	 */
	public HashMap<String, CategoryList> getCategoryLists() {
		if (pictures == null || movies == null || list == null) {
			
			URL categoryListURL = null;
			try {
				categoryListURL = new URL("http://www.ampland.com/mmpage.html");
				//categoryListURL = new URL("file:///home/freezzaph/freezzaph/plugins/ampland/mmpage.html");
			} catch (MalformedURLException e) {
				// Won't happen
			}
			
			// Download list with categories
			String data;
			try {
				data = URLFetcher.fetch(categoryListURL);
			} catch (URLFetchException e) {
				throw new PluginException("Could not download category list. Ampland.com may not be accessible, or the plugin may need to be updated.", e);
			}
			
			// Create category lists
			pictures = new CategoryList("pictures", "Picture galleries");
			movies = new CategoryList("movies", "Movie galleries");
			
			Matcher m = pictureCategory.matcher(data);
			while (m.find()) {
				
				// Extract data from match
				String decodedName = HTMLEntityDecoder.decode(m.group(2));
				String name = Category.prettify(decodedName);
				String description = decodedName;
				
				try {
					URL categoryURL = new URL(categoryListURL, m.group(1));
					
					// Create picture categories
					AmplandPictureCategory ampPic = new AmplandPictureCategory(name, description, categoryURL);
					pictures.addCategory(name, ampPic);
				} catch (MalformedURLException murle) {
					// Malformed URL, ignore
				}
			}
			
			m = movieCategory.matcher(data);
			while (m.find()) {
				
				// Extract data from match
				String decodedName = HTMLEntityDecoder.decode(m.group(2));
				String name = Category.prettify(decodedName);
				String description = decodedName;
				
				try {
					URL categoryURL = new URL(categoryListURL, m.group(1));
					
					// Create picture categories
					AmplandMovieCategory ampPic = new AmplandMovieCategory(name, description, categoryURL);
					movies.addCategory(name, ampPic);
				} catch (MalformedURLException murle) {
					// Malformed URL, ignore
				}
			}

			// Create list over category lists
			list = new HashMap<String, CategoryList>(2);
			list.put("pictures", pictures);
			list.put("movies", movies);
		}
		
		return list;
	}

	/* (non-Javadoc)
	 * @see freezzaph.plugins.FreeZzaphPlugin#getPluginName()
	 */
	public String getPluginName() {
		return "ampland";
	}

	/* (non-Javadoc)
	 * @see freezzaph.plugins.FreeZzaphPlugin#getPluginTitle()
	 */
	public String getPluginTitle() {
		return "Ampland.com";
	}

}
